﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace Quanlytruongtruongtieuhoc
{
    class clsHinhanh
    {
        public static void StoreImage(string filename,string ma)
        {
            byte[] imageData = null;
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) 
            {
                imageData = new Byte[fs.Length];
                fs.Read(imageData, 0, (int)fs.Length);
            }
            SqlCommand com = new SqlCommand("Luuhinh", clsKetnoi.con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@ma_gv", ma);
            com.Parameters["@ma_gv"].Direction = ParameterDirection.Input;
            com.Parameters.Add("@hinh", SqlDbType.Image);
            com.Parameters["@hinh"].Direction = ParameterDirection.Input;
            com.Parameters["@hinh"].Value = imageData;
            com.ExecuteNonQuery();
        }
        public static void StoreImage1(string filename, string ma)
        {
            byte[] imageData = null;
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                imageData = new Byte[fs.Length];
                fs.Read(imageData, 0, (int)fs.Length);
            }
            SqlCommand com = new SqlCommand("Luuhinhhs", clsKetnoi.con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@ma_hs", ma);
            com.Parameters["@ma_hs"].Direction = ParameterDirection.Input;
            com.Parameters.Add("@hinh", SqlDbType.Image);
            com.Parameters["@hinh"].Direction = ParameterDirection.Input;
            com.Parameters["@hinh"].Value = imageData;
            com.ExecuteNonQuery();
        }
        public static Image ByteArrayToImage(byte[] bytes)
        {
            if (bytes != null)
            {
                MemoryStream stream = new MemoryStream(bytes);
                return Bitmap.FromStream(stream);
            }
            else
            {
                return null;
            }
        }
    }
}
